package sanglh.edu.hsphysics.utils;

import android.content.Context;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.GridView;
import android.widget.ListView;

public class CommonUtils {
	public static void listAnimation(ListView lv, View row, int position) {
		int offset = Math.abs((position - lv.getFirstVisiblePosition())) * 100;
		if (offset > 500) {
			offset = 500;
		}
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1, 0.5f, 1,
				0.5f, 0.5f);
		animationSet.addAnimation(alphaAnimation);
		animationSet.addAnimation(scaleAnimation);
		animationSet.setDuration(offset);
		animationSet.setFillAfter(true);
		row.startAnimation(animationSet);
	}

	public static void gridAnimation(GridView gv, View row, int position) {
		int offset = (position % gv.getNumColumns() + position
				/ gv.getNumColumns()) * 100;
		if (offset > 500) {
			offset = 500;
		}
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1, 0.5f, 1,
				0.5f, 0.5f);
		animationSet.addAnimation(alphaAnimation);
		animationSet.addAnimation(scaleAnimation);
		animationSet.setDuration(offset);
		animationSet.setFillAfter(true);
		row.startAnimation(animationSet);
	}

	public static void visibleAnimation(View view) {
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		animationSet.addAnimation(alphaAnimation);
		animationSet.setDuration(300);
		animationSet.setFillAfter(true);
		view.startAnimation(animationSet);
	}

	public static void invisibleAnimation(View view) {
		if (view == null) {
			return;
		}
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
		animationSet.addAnimation(alphaAnimation);
		animationSet.setDuration(300);
		animationSet.setFillAfter(true);
		view.startAnimation(animationSet);
	}

	public static int getResource(Context context, String name) {
		return context.getResources().getIdentifier(name, "drawable",
				context.getPackageName());
	}


}
